package edu.zzu.servideImpl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.zzu.Entity.Package;
import edu.zzu.Entity.PackageDetail;
import edu.zzu.dao.PackageDao;
import edu.zzu.dao.RegionDao;
import edu.zzu.myUtil.PackageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PackageService implements edu.zzu.service.PackageService {

    @Autowired
    private PackageDao packageDao;

    @Autowired
    private PackageUtil packageUtil;

    @Autowired
    private ObjectMapper jsonMapper;

    @Autowired
    private RegionDao regionDao;

    @Autowired
    private PackageRouteService packageRouteService;

    @Override
    public String getPackageBySenderTel(String tel) {
        try {
            return jsonMapper.writeValueAsString(packageDao.getPackageBySenderTel(tel));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getPackageDetailBySenderTel(String tel) {
        List<Package> packageByReceiverTel = packageDao.getPackageBySenderTel(tel);
        List<PackageDetail> res=new ArrayList<>();

        for (Package aPackage : packageByReceiverTel) {
            PackageDetail packageDetail=new PackageDetail();
            packageDetail.setId(aPackage.getId());
            packageDetail.setSourceNode(aPackage.getSourceNode());
            packageDetail.setTargetNode(aPackage.getTargetNode());
            packageDetail.setUpdateTime(aPackage.getUpdateTime());
            packageDetail.setStatus(aPackage.getStatus());
            packageDetail.setSender(aPackage.getSender());
            packageDetail.setReceiver(aPackage.getReceiver());
            packageDetail.setSenderTel(aPackage.getSenderTel());
            packageDetail.setReceiverTel(aPackage.getReceiverTel());
            packageDetail.setHandler(aPackage.getHandler());
            packageDetail.setUserId(aPackage.getUserId());
            packageDetail.setTransNode(packageRouteService.getAddressByTransNodeId(aPackage.getTransNodeId()));
            packageDetail.setSenderAddr(aPackage.getSenderAddr());
            packageDetail.setReceiverAddr(aPackage.getReceiverAddr());
            packageDetail.setX(aPackage.getX());
            packageDetail.setY(aPackage.getY());

            res.add(packageDetail);

        }
        try {
            return jsonMapper.writeValueAsString(res);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getPackageByReceiverTel(String tel) {
        try {
            return jsonMapper.writeValueAsString(packageDao.getPackageByReceiverTel(tel));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getPackageDetailByReceiverTel(String tel) {
        List<Package> packageByReceiverTel = packageDao.getPackageByReceiverTel(tel);
        List<PackageDetail> res=new ArrayList<>();

        for (Package aPackage : packageByReceiverTel) {
            PackageDetail packageDetail=new PackageDetail();
            packageDetail.setId(aPackage.getId());
            packageDetail.setSourceNode(aPackage.getSourceNode());
            packageDetail.setTargetNode(aPackage.getTargetNode());
            packageDetail.setUpdateTime(aPackage.getUpdateTime());
            packageDetail.setStatus(aPackage.getStatus());
            packageDetail.setSender(aPackage.getSender());
            packageDetail.setReceiver(aPackage.getReceiver());
            packageDetail.setSenderTel(aPackage.getSenderTel());
            packageDetail.setReceiverTel(aPackage.getReceiverTel());
            packageDetail.setHandler(aPackage.getHandler());
            packageDetail.setUserId(aPackage.getUserId());
            packageDetail.setTransNode(packageRouteService.getAddressByTransNodeId(aPackage.getTransNodeId()));
            packageDetail.setSenderAddr(aPackage.getSenderAddr());
            packageDetail.setReceiverAddr(aPackage.getReceiverAddr());
            packageDetail.setX(aPackage.getX());
            packageDetail.setY(aPackage.getY());

            res.add(packageDetail);

        }
        try {
            return jsonMapper.writeValueAsString(res);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean savePackage(String id, String sourceNode, String targetNode, String updateTime, Integer status, String sender,
                               String receiver, String senderTel, String receiverTel, Long handler, Long userId, Long transNodeId,
                               String senderAddr, String receiverAddr,Float x,Float y) {
        if(sourceNode.equals("")){
            int beginIndex=senderAddr.indexOf("市");
            int endIndex=senderAddr.indexOf("区");
            if (endIndex==-1) {
                endIndex=senderAddr.indexOf("县");
            }
            sourceNode=regionDao.getIdByTwn(senderAddr.substring(beginIndex+1,endIndex+1));
        }

        if(targetNode.equals("")){
            int beginIndex=receiverAddr.indexOf("市");
            int endIndex=receiverAddr.indexOf("区");
            if (endIndex==-1) {
                endIndex=receiverAddr.indexOf("县");
            }
            targetNode=regionDao.getIdByTwn(receiverAddr.substring(beginIndex+1,endIndex+1));
        }

        if (updateTime.equals("")) {
            updateTime=new Date(System.currentTimeMillis())+" "+new Time(System.currentTimeMillis());
        }

        return packageDao.savePackage(new Package(id,sourceNode,targetNode,updateTime,status,sender,
                receiver,senderTel,receiverTel,handler,userId,transNodeId,senderAddr,receiverAddr,x,y))==1?true:false;
    }

    @Override
    public String produceRandomPackageId() {
        return packageUtil.produceRandomPackageId();
    }

    @Override
    public boolean updatePackageStatusById(String id, Integer status,Long handler,Long transNodeId,Float x,Float y) {
        return packageDao.updatePackageStatus(id,status,handler,transNodeId,x,y);
    }

    @Override
    public String getDLSTask(String regionCode) {
        try {
            return jsonMapper.writeValueAsString(packageDao.selectDLSByRegionCode(regionCode));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getDeliveryTaskByHandler(Long handler) {
        try {
            return jsonMapper.writeValueAsString(packageDao.selectDeliveryByHandler(handler));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getPackageById(String id) {
        try {
            return jsonMapper.writeValueAsString(packageDao.selectPackageById(id));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String getPackageDetailById(String id) {

        Package aPackage = packageDao.selectPackageById(id);

        PackageDetail packageDetail=new PackageDetail();

        packageDetail.setId(aPackage.getId());
        packageDetail.setSourceNode(aPackage.getSourceNode());
        packageDetail.setTargetNode(aPackage.getTargetNode());
        packageDetail.setUpdateTime(aPackage.getUpdateTime());
        packageDetail.setStatus(aPackage.getStatus());
        packageDetail.setSender(aPackage.getSender());
        packageDetail.setReceiver(aPackage.getReceiver());
        packageDetail.setSenderTel(aPackage.getSenderTel());
        packageDetail.setReceiverTel(aPackage.getReceiverTel());
        packageDetail.setHandler(aPackage.getHandler());
        packageDetail.setUserId(aPackage.getUserId());
        packageDetail.setTransNode(packageRouteService.getAddressByTransNodeId(aPackage.getTransNodeId()));
        packageDetail.setSenderAddr(aPackage.getSenderAddr());
        packageDetail.setReceiverAddr(aPackage.getReceiverAddr());
        packageDetail.setX(aPackage.getX());
        packageDetail.setY(aPackage.getY());

        try {
            return jsonMapper.writeValueAsString(packageDetail);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Boolean checkPackageExistById(String id) {
        return packageDao.checkPackageExistById(id);
    }

    @Override
    public Long getPackageAmountByTransNodeId(Long id) {
        return packageDao.selectPackageAmountByTransNodeId(id);
    }

    @Override
    public String getAllPackagesOfTransNode(Long id) {

        try {
            return jsonMapper.writeValueAsString(packageDao.selectPackagesOfTransNodeId(id));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getPackagesByStatus(Integer status) {
        try {
            return jsonMapper.writeValueAsString(packageDao.selectPackagesByStatus(status));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
